/*
 * ThreadHandler.h
 *
 *  Created on: 2012-12-18
 *      Author: yangentao@gmail.com
 */

#pragma once;
#include <windows.h>
#include <list>
#include <exception>
#include "AutoLock.h"
#include "Thread.h"
#include "Event.h"
#include "String.h"
#include "MessageQueue.h"

using namespace std;

namespace yet {

class ThreadHandler: public ThreadLock {
public:
	ThreadHandler();
	virtual ~ThreadHandler();
public:
	void post(Message msg, bool front = false);
	void postFront(Message msg);
	void postQuitMessage();
	bool start();
	void quit();
	bool isActive();
	DWORD waitFinish(DWORD millSeconds = INFINITE);
	void setPriority(Thread::Priority p);
private:
	DWORD onLoop(void* arg);
	void notify();
	void processMsg();
	void cancelMsg();
public:
	boost::function<void(Message)> OnMessage;
	boost::function<void(Message)> OnCancel;
protected:
	MessageQueue messages;
	bool volatile active;
	Thread thread;
	AutoEvent event;

};

} /* namespace yet */
